package com.taotao.common.auth.satoken;

import com.google.common.base.Preconditions;
import com.taotao.common.auth.LoginService;
import com.taotao.common.auth.UserInfo;
import com.taotao.common.redis.RedisClient;
import com.taotao.sys.model.po.SysUserAccount;
import com.taotao.sys.model.vo.login.LoginParam;
import com.taotao.sys.service.SysAccountRoleService;
import com.taotao.sys.service.SysFuncService;
import com.taotao.sys.service.SysUserAccountService;
import com.taotao.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2022/5/5 17:13
 */
@Slf4j
@Service
public class SaTokenLoginService implements LoginService {
    @Autowired
    private SysUserAccountService sysUserAccountService;


    @Autowired
    private RedisClient redisClient;
    @Autowired
    private SysAccountRoleService sysAccountRoleService;

    @Autowired
    private SysFuncService sysFuncService;


    @Override
    public UserInfo getLoginUserInfo(String token) {
        Preconditions.checkArgument(StringUtils.isNotBlank(token));

        return SatokenUtils.getUserInfo();
    }

    @Override
    public void clearLoginUserInfo(Long userId) {
        Preconditions.checkArgument(null != userId);
        // sa-token 不需要清理

    }

    @Override
    public String login(LoginParam param) throws LoginException {

        Preconditions.checkArgument(null != param);


        /**
         * 首先调用 StpUtil.login(id) 进行登录
         * 调用 StpUtil.getTokenInfo() 返回当前会话的token详细参数
         * 此方法返回一个对象，其有两个关键属性：tokenName和tokenValue（token 的名称和 token 的值）
         * 将此对象传递到前台，让前端人员将这两个值保存到本地
         *
         *
         * 前端将 token 提交到后端
         * 无论是app还是小程序，其传递方式都大同小异
         * 那就是，将 token 塞到请求header里 ，格式为：{tokenName: tokenValue}
         */


        SysUserAccount userAccount = sysUserAccountService.queryByAccount(param.getUserName());
        if (null == userAccount || !PasswordUtil.checkPass(param.getPassword(), userAccount.getPassword())) {
            throw new LoginException("账号密码错误");
        }


        return SatokenUtils.login(getUserInfo(userAccount.getId()),new HashMap<>());
    }

    @Override
    public UserInfo getUserInfo(Long userId) {
        Preconditions.checkArgument(null != userId);

        SysUserAccount account = sysUserAccountService.selectById(userId);
        if (null == account) {
            return null;
        }

        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(account, userInfo);
        return userInfo;
    }

    @Override
    public Set<String> getUserFuncList(Long userId) {
        Preconditions.checkArgument(null != userId);

        Set<Long> roleIds = sysAccountRoleService.queryByUserId(userId);

        if (CollectionUtils.isEmpty(roleIds)) {
            return null;
        }

        return sysFuncService.queryFuncCodeByRoleIds(roleIds);
    }

}
